/**
 @文件名： layoutInfo
 @来源：YongPing.Wang|2023/11/17 15:25
 @描述：平台框架储存信息
 **/

import { defineStore } from 'pinia'
import baseParam from '@/config/parameter/baseParam.ts'
import { getMenuApi } from '@/api/menuApi.ts'
import { type IApi } from '@/api/types.ts'

export interface IMenu {
  id: string
  menuName: string
  parentId: string
  icon: string
  componentUrl: string
  sortId: string
  memo: string
  isCheck: string
  edit: boolean
  children?: IMenu[]
}

interface IMenuInfo {
  menuList: IMenu[]
}

export const menuInfo = defineStore({
  id: `${baseParam.systemDefaultPrefix}_MENU_INFO`,
  state: (): IMenuInfo => {
    return {
      menuList: [],
    }
  },
  actions: {
    // 获取菜单
    async getMenu(): Promise<IMenu[]> {
      const data = await getMenuApi().then((res: IApi<IMenu[]>) => {
        return res.data || []
      })
      // todo 后面删掉
      data.forEach((item: IMenu) => {
        item.icon = 'Menu'
        item.componentUrl = `/root${item.componentUrl}`
      })
      this.menuList = data
      return data
    },
    // 清空 信息
    removeMenuInfo() {
      this.$reset()
    },
  },
  persist: true,
})
